package com.ruoyi.wgjlcData.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.pad.LinePoint;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.calculate.BezierCurveUtil;
import com.ruoyi.wgjlcData.domain.plat.DataWgjlcZhdjlgps;
import com.ruoyi.wgjlcData.domain.vo.TestZhdjl;
import com.ruoyi.wgjlcData.mapper.DataWgjlcZhdjlgpsMapper;
import com.ruoyi.wgjlcData.service.IDataWgjlcZhdjlgpsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;


/**
 * 无轨胶轮车制动距离测试项Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-27
 */
@Service
public class DataWgjlcZhdjlgpsServiceImpl implements IDataWgjlcZhdjlgpsService
{
    @Autowired
    private DataWgjlcZhdjlgpsMapper dataWgjlcZhdjlgpsMapper;

    /**
     * 查询无轨胶轮车制动距离测试项
     * 
     * @param zhdjlgpsId 无轨胶轮车制动距离测试项主键
     * @return 无轨胶轮车制动距离测试项
     */
    @Override
    public DataWgjlcZhdjlgps selectDataWgjlcZhdjlgpsByZhdjlgpsId(Long zhdjlgpsId)
    {
        return dataWgjlcZhdjlgpsMapper.selectDataWgjlcZhdjlgpsByZhdjlgpsId(zhdjlgpsId);
    }

    /**
     * 查询无轨胶轮车制动距离测试项列表
     * 
     * @param dataWgjlcZhdjlgps 无轨胶轮车制动距离测试项
     * @return 无轨胶轮车制动距离测试项
     */
    @Override
    public List<DataWgjlcZhdjlgps> selectDataWgjlcZhdjlgpsList(DataWgjlcZhdjlgps dataWgjlcZhdjlgps)
    {
        return dataWgjlcZhdjlgpsMapper.selectDataWgjlcZhdjlgpsList(dataWgjlcZhdjlgps);
    }

    /**
     * 新增无轨胶轮车制动距离测试项
     * 
     * @param dataWgjlcZhdjlgps 无轨胶轮车制动距离测试项
     * @return 结果
     */
    @Override
    public int insertDataWgjlcZhdjlgps(DataWgjlcZhdjlgps dataWgjlcZhdjlgps)
    {
        dataWgjlcZhdjlgps.setCreateTime(DateUtils.getNowDate());
        return dataWgjlcZhdjlgpsMapper.insertDataWgjlcZhdjlgps(dataWgjlcZhdjlgps);
    }

    /**
     * 修改无轨胶轮车制动距离测试项
     * 
     * @param dataWgjlcZhdjlgps 无轨胶轮车制动距离测试项
     * @return 结果
     */
    @Override
    public int updateDataWgjlcZhdjlgps(DataWgjlcZhdjlgps dataWgjlcZhdjlgps)
    {
        dataWgjlcZhdjlgps.setUpdateTime(DateUtils.getNowDate());
        //list转Json字符串
        if(!dataWgjlcZhdjlgps.getTestZhdjlList().isEmpty()) {
            dataWgjlcZhdjlgps.setDataList(JSON.toJSONString(dataWgjlcZhdjlgps.getTestZhdjlList()));
        }
        return dataWgjlcZhdjlgpsMapper.updateDataWgjlcZhdjlgps(dataWgjlcZhdjlgps);
    }

    /**
     * 查询无轨胶轮车-制动距离测试项
     *
     * @param dataTaskId
     * @return 无轨胶轮车-制动距离测试项
     */
    @Async("threadPoolKy")
    @Override
    public Future<List<DataWgjlcZhdjlgps>> selectDataWgjlcZhdjlgpsByDataTaskId(Long dataTaskId) {
        List<DataWgjlcZhdjlgps> dataWgjlcZhdjlgpsEntities = dataWgjlcZhdjlgpsMapper.selectDataWgjlcZhdjlgpsByDataTaskId(dataTaskId);
        if (dataWgjlcZhdjlgpsEntities.size() != 0) {
            dataWgjlcZhdjlgpsEntities.forEach(item -> {
                if (item.getDataList() != null && item.getDataList().length() != 0) {
                    item.setTestZhdjlList(JSON.parseArray(item.getDataList(), TestZhdjl.class));
                    item.setListSize(item.getTestZhdjlList().size());
                    //判断是否存在点位
                    if(!item.getTestZhdjlList().isEmpty()) {
                        item.getTestZhdjlList().forEach(i -> {
                            if(!i.getPointList().isEmpty()) {
                                List<Float> xVelList = new ArrayList<>(); List<Float> yVelList = new ArrayList<>();
                                for (LinePoint linePoint : i.getPointList()) {
                                    xVelList.add(linePoint.getX());
                                    yVelList.add(linePoint.getY());
                                }
                                i.setSdChartLineDTO(BezierCurveUtil.calPoints(xVelList, yVelList, true));
                            }
                        });
                    }
                }
            });
        }
        return new AsyncResult<>(dataWgjlcZhdjlgpsEntities);
    }

    /**
     * 修改状态为2
     *
     * @param dataTaskId
     */
    @Override
    public int updateDataWgjlcZhdjlgpsByDataTaskId(Long dataTaskId) {
        return dataWgjlcZhdjlgpsMapper.updateDataWgjlcZhdjlgpsByDataTaskId(dataTaskId);
    }

    /**
     * 批量新增无轨胶轮车-制动距离测试项
     *
     * @param dataWgjlcZhdjlgps 无轨胶轮车-制动距离测试项
     * @return 结果
     */
    @Override
    public int batchDataWgjlcZhdjlgps(List<DataWgjlcZhdjlgps> dataWgjlcZhdjlgps) {
        return dataWgjlcZhdjlgpsMapper.batchDataWgjlcZhdjlgps(dataWgjlcZhdjlgps);
    }

    /**
     * 批量删除无轨胶轮车制动距离测试项
     * 
     * @param zhdjlgpsIds 需要删除的无轨胶轮车制动距离测试项主键
     * @return 结果
     */
    @Override
    public int deleteDataWgjlcZhdjlgpsByZhdjlgpsIds(Long[] zhdjlgpsIds)
    {
        return dataWgjlcZhdjlgpsMapper.deleteDataWgjlcZhdjlgpsByZhdjlgpsIds(zhdjlgpsIds);
    }

    /**
     * 删除无轨胶轮车制动距离测试项信息
     * 
     * @param zhdjlgpsId 无轨胶轮车制动距离测试项主键
     * @return 结果
     */
    @Override
    public int deleteDataWgjlcZhdjlgpsByZhdjlgpsId(Long zhdjlgpsId)
    {
        return dataWgjlcZhdjlgpsMapper.deleteDataWgjlcZhdjlgpsByZhdjlgpsId(zhdjlgpsId);
    }
}
